package com.yh.consumer.Controller;

import com.yh.consumer.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 简单通过restTemplate调用远程微服务API
     * 访问地址：http://localhost:8001/user/1
     * @param id
     * @return
     */
    @GetMapping("user/{id}")
    public User findOneUser(@PathVariable Long id){
        return  restTemplate.getForObject("http://localhost:8000/findOneUser/"+id,User.class);
    }

    /**
     * 通过使用discoveryClient 获取提供者微服务的应用信息（包含自定义元数据）
     * @return
     */
    @GetMapping("user/instance")
    public List<ServiceInstance> showInfo(){
        return discoveryClient.getInstances("spring-cloud-user-provider");
    }
}
